# **********************************************************************
#
#  Audacity: A Digital Audio Editor
#
# **********************************************************************

cmake_minimum_required(VERSION 3.24)

cmake_policy(SET CMP0091 OLD) # not set MSVC default args

project(audacity LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

if(APPLE)
    if(NOT CMAKE_OSX_ARCHITECTURES)
        set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64" CACHE STRING "macOS build architectures" FORCE)
    endif()
endif()

###########################################
# Muse Framework
###########################################
set(FETCHCONTENT_QUIET OFF)
set(FETCHCONTENT_BASE_DIR ${PROJECT_BINARY_DIR}/_deps)

set(MUSE_FRAMEWORK_PATH ${CMAKE_SOURCE_DIR}/muse_framework)
set(MUSE_FRAMEWORK_SRC_PATH ${MUSE_FRAMEWORK_PATH}/framework)

set(CMAKE_MODULE_PATH
    ${CMAKE_CURRENT_LIST_DIR}
    ${CMAKE_CURRENT_LIST_DIR}/buildscripts/cmake
    ${MUSE_FRAMEWORK_PATH}/buildscripts/cmake
    ${CMAKE_MODULE_PATH}
    )

include(${MUSE_FRAMEWORK_SRC_PATH}/cmake/MuseDeclareOptions.cmake)

###########################################
# AudacityDependencies
###########################################
set(AUDACITY_ROOT ${CMAKE_CURRENT_LIST_DIR}/au3)
add_definitions(-DINSTALL_PREFIX="${CMAKE_INSTALL_PREFIX}")

include(GetPlatformInfo)

include(SetupDependencies)

###########################################
# Setup option and build settings
###########################################
include(GetPaths)

set(AU4_BUILD_CONFIGURATION "app" CACHE STRING "Build configuration")
# Possible values:
# - app             - for desktop app
# - app-portable    - for desktop portable app (Windows build for PortableApps.com)
# - utest           - for unit tests (for CI)

set(AU4_BUILD_MODE "dev" CACHE STRING "Build mode")
# Possible values:
# - dev     - for development/nightly builds
# - testing - for testing versions (alpha, beta, RC)
# - release - for stable release builds

set(AU4_REVISION "" CACHE STRING "Build revision")

set(MUSE_ENABLE_UNIT_TESTS ON)
set(MUSE_MODULE_ACCESSIBILITY ON)
set(MUSE_MODULE_ACTIONS ON)
set(MUSE_MODULE_AUDIO OFF)
set(MUSE_MODULE_AUDIOPLUGINS ON)
set(MUSE_MODULE_AUTOBOT ON)
set(MUSE_MODULE_CLOUD ON)
set(MUSE_MODULE_CLOUD_MUSESCORECOM OFF CACHE BOOL "Enable MuseScore.com account" FORCE)
set(MUSE_MODULE_DIAGNOSTICS ON)
set(MUSE_MODULE_DRAW ON)
set(MUSE_MODULE_DRAW_TESTS OFF)
set(MUSE_MODULE_EXTENSIONS ON)
set(MUSE_MODULE_GLOBAL ON)
set(MUSE_MODULE_GLOBAL_TESTS OFF)
set(MUSE_MODULE_LANGUAGES ON)
set(MUSE_MODULE_LEARN ON)
set(MUSE_MODULE_MIDI OFF)
set(MUSE_MODULE_MPE OFF)
set(MUSE_MODULE_MULTIINSTANCES OFF)
set(MUSE_MODULE_MUSESAMPLER OFF)
set(MUSE_MODULE_NETWORK ON)
set(MUSE_MODULE_SHORTCUTS ON)
set(MUSE_MODULE_UI ON)
set(MUSE_MODULE_UI_TESTS OFF)
set(MUSE_MODULE_UPDATE OFF)
set(MUSE_MODULE_WORKSPACE ON)

# Modules (alphabetical order please)
option(AU_BUILD_APPSHELL_MODULE "Build appshell module" ON)
option(AU_BUILD_CONTEXT_TESTS "Build context tests" ON)
option(AU_BUILD_EFFECTS_BUILTIN_TESTS "Build builtin-effect tests" ON)
option(AU_BUILD_EFFECTS_MODULE "Build effects module" ON)
option(AU_BUILD_EFFECTS_TESTS "Build effects tests" ON)
option(AU_BUILD_PLAYBACK_MODULE "Build playback module" ON)
option(AU_BUILD_PLAYBACK_TESTS "Build playback tests" ON)
option(AU_BUILD_PROJECT_TESTS "Build project tests" ON)
option(AU_BUILD_PROJECTSCENE_MODULE "Build projectscene modules" ON)
option(AU_BUILD_PROJECTSCENE_TESTS "Build projectscene tests" ON)
option(AU_BUILD_RECORD_MODULE "Build record module" ON)
option(AU_BUILD_RECORD_TESTS "Build record tests" ON)
option(AU_BUILD_UICOMPONENTS_TESTS "Build uicomponents tests" ON)
option(AU_BUILD_TRACKEDIT_TESTS "Build trackedit tests" ON)

option(AU_MODULE_EFFECTS_VST "Build audacity vst module" ON)
set(AU_MODULE_VST_VST3_SDK_PATH "" CACHE PATH "[optional] Path to VST3_SDK. SDK version >= ${VST3_SDK_VERSION} required")

if (OS_IS_LIN)
    option(AU_MODULE_EFFECTS_LV2 "Build audacity lv2 module" ON)
endif(OS_IS_LIN)

if( CMAKE_SYSTEM_NAME MATCHES "Darwin" )
    option(AU_MODULE_EFFECTS_AUDIO_UNIT "Build Audacity Audio Unit module" ON)
endif()

# === Setup ===

# === Pack ===
option(MU_RUN_LRELEASE "Generate .qm files" ON)

# === Compile ===
option(MU_COMPILE_INSTALL_QTQML_FILES "Whether to bundle qml files along with the installation (relevant on MacOS only)" ON)
option(MUSE_COMPILE_USE_UNITY "Use unity build." ON)
option(MUSE_COMPILE_USE_CCACHE "Try use ccache" ON)

###########################################
# Setup Configure
###########################################

# default
set(QT_ADD_LINGUISTTOOLS ON)
set(QT_ADD_CONCURRENT ON)
set(QT_QPROCESS_SUPPORTED ON)

if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/SetupConfigure.local.cmake")
    include(${CMAKE_CURRENT_LIST_DIR}/SetupConfigure.local.cmake)
else()
    include(SetupConfigure)
endif()

###########################################
# Setup compiler and build environment
###########################################

include(SetupBuildEnvironment)
include(GetPlatformInfo)
include(SetupDevEnvironment)

if (MUE_COMPILE_USE_CCACHE)
    include(TryUseCcache)
endif(MUE_COMPILE_USE_CCACHE)


###########################################
# Setup external dependencies
###########################################
set(QT_MIN_VERSION "6.2.4")
include(SetupQt6)

###########################################
# Add source tree
###########################################
if (MUSE_ENABLE_UNIT_TESTS)
    enable_testing()
    message(STATUS "Enabled testing")
endif()

add_subdirectory(${MUSE_FRAMEWORK_SRC_PATH})
add_subdirectory(src)
add_subdirectory(share)

if (MSVC)
    # Now that the `audacity` target is defined, we can set it as the startup project in Visual Studio.
    set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT audacity)
endif()

###########################################
# Setup Packaging
###########################################
if (OS_IS_LIN)
    include(${CMAKE_CURRENT_LIST_DIR}/buildscripts/packaging/Linux+BSD/SetupAppImagePackaging.cmake)
endif(OS_IS_LIN)

if (OS_IS_WIN)
    include(${CMAKE_CURRENT_LIST_DIR}/buildscripts/packaging/Windows/SetupWindowsPackaging.cmake)
endif(OS_IS_WIN)
